# -*- coding:utf-8 -*-
__author__: 'Rvelamen'
__data__ = '2022/3/18 1:37'

from app.db.session import engine
from app.common.logger import logger
from app.models.base import BaseModel

from app.models.models import City, Data  # 必须填写
from app.models.user import User


# async def init_table():
#     async with engine.begin() as conn:
#         await conn.run_sync(Base.metadata.drop_all)
#         await conn.run_sync(Base.metadata.create_all)
#
#
# if __name__ == '__main__':
#     logger.info("数据库初始化!!!")
#     loop = asyncio.get_event_loop()
#     loop.run_until_complete(init_table())
#     logger.info("数据库初始化成功!!!")

def init_db():
    """ 创建 db/base 下的所有表 """
    try:
        drop_db()  # 删除所有的表
        BaseModel.metadata.create_all(bind=engine)
        logger.info("创建表成功!!!")
    except Exception as e:
        logger.error(f"创建表失败!!! -- 错误信息如下:\n{e}")


def drop_db():
    """ 删除 db/base 下的所有表 """
    try:
        BaseModel.metadata.drop_all(bind=engine)
        logger.info("删除表成功!!!")
    except Exception as e:
        logger.error(f"删除表失败!!! -- 错误信息如下:\n{e}")
